<?php

	require(ABSPATH.'/wp-includes/pluggable.php');
	require(ABSPATH.'/wp-includes/class-phpmailer.php');
	//require('PHPMailer-master/PHPMailerAutoload.php');
	# PAGE SETTING
	 $class_nam   ='';
	 $add_function   ='';
	class trackClass
	{
		private $myFields=array("id","track_status","assigned_user");
		
		function updtrack($tblname,$meminfo,$profile_picture='')
		{
			global $wpdb;
		 	$count = sizeof($meminfo);
		
			if($count>0)
			{
				$field="";
				$vals="";
				foreach($this->myFields as $key)
				{
					if($field=="" && $key!="id")
					{
						$field="`".$key."` = '".$meminfo[$key]."'";
					}
					else if($key!="id")
					{
						$field=$field.",`".$key."` = '".$meminfo[$key]."'";
					}
				}
				// PROFILE PICTURE
				if($profile_picture!=''){
					
					$field=$field.",`image` = '".$profile_picture."'";
				}
				$sSQL = "update ".$tblname." set $field where id=".$meminfo["id"];
				$wpdb->query($sSQL);
				return true;
			}
			else
			{
				return false;
			}
		}
		
		function getTrackStatus($track_status){
				if($track_status == 1)	
					$track_status  = 'Pending';
				if($track_status == 2)	
					$track_status  = 'Photographer Assigned';	
				if($track_status == 3)	
					$track_status  = 'Shoot Scheduled';
				if($track_status == 4)	
					$track_status  = 'Shoot Completed';
				if($track_status == 5)	
					$track_status  = 'Design in Progress';
				if($track_status == 6)	
					$track_status  = 'Tour Complete';
				if($track_status == 7)	
					$track_status  = 'Order Cancelled';	
			return $track_status;
			}
		
		function getTrackSelect($val){
			
			   $html = '<option value="8" '.($val==8 ? 'selected' : '').'>All Active Projects</option>
						<option value="0" '.($val==0 ? 'selected' : '').'>All Projects</option>
						<option value="1" '.($val==1 ? 'selected' : '').'>Pending</option>
						<option value="2" '.($val==2 ? 'selected' : '').'>Photographer Assigned</option>
						<option value="3" '.($val==3 ? 'selected' : '').'>Photo Shoot Scheduled</option>
						<option value="4" '.($val==4 ? 'selected' : '').'>Photo Shoot Completed</option>
						<option value="5" '.($val==5 ? 'selected' : '').'>Design in Progress</option>
						<option value="6" '.($val==6 ? 'selected' : '').'>Tour Complete</option>
						<option value="7" '.($val==7 ? 'selected' : '').'>Cancel Order</option>';
			   return $html;
			}
		
		function assign_photographer($assigned_by,$assigned_to,$order_id,$track_status=0){
		
		$query_cart = mysql_query('SELECT id FROM wp_spec_assigned_orders WHERE order_id="'.$order_id.'" AND assigned_to ="'.$assigned_to.'"');
			if (mysql_num_rows($query_cart) <= 0 )
			{
			mysql_query("INSERT INTO `wp_spec_assigned_orders` (
							`id` ,
							`order_id` ,
							`assigned_by` ,
							`assigned_to` ,
							`time`
							)
							VALUES (
							NULL ,  '".$order_id."',  '".$assigned_by."',  '".$assigned_to."', 
							CURRENT_TIMESTAMP
							);");
			
			}
		}
		
		function reassignProject($order_id,$assigned_to,$privious_assigned){
		mysql_query("UPDATE  `wp_spec_assigned_orders` SET  `assigned_to` =  '".$assigned_to."' WHERE  `wp_spec_assigned_orders`.`order_id` ='".$order_id."' AND assigned_to = '".$privious_assigned."'");
		
		}
		
		function makeClickableLinks($s) {
		
		  return preg_replace('@(https?://([-\w\.]+[-\w])+(:\d+)?(/([\w/_\.#-]*(\?\S+)?[^\.\s])?)?)@', '<a href="$1" target="_blank">$1</a>', $s);
		}
		
		
		
		function assign_project($assigned_by,$assigned_to,$order_id,$track_status=0){
		
		if($track_status>0){
			$this->update_status_time($order_id,$track_status);
			mysql_query("UPDATE  `wp_spec_order_trac` SET  `track_status` =  ".$track_status." WHERE  `wp_spec_order_trac`.`order_id` =".$order_id.";");
		}	
		
		if($assigned_to!=0){				
				$query_cart = mysql_query('SELECT id FROM wp_spec_assigned_orders WHERE order_id="'.$order_id.'" AND assigned_to ="'.$assigned_to.'"');
					if (mysql_num_rows($query_cart) <= 0 )
					{
					mysql_query("INSERT INTO `wp_spec_assigned_orders` (
									`id` ,
									`order_id` ,
									`assigned_by` ,
									`assigned_to` ,
									`time`
									)
									VALUES (
									NULL ,  '".$order_id."',  '".$assigned_by."',  '".$assigned_to."', 
									CURRENT_TIMESTAMP
									);");
					
					}
					
				$query_trac = mysql_query('SELECT id FROM wp_spec_order_trac WHERE order_id="'.$order_id.'"');	
					if (mysql_num_rows($query_trac) <= 0 ){
					mysql_query("INSERT INTO `wp_spec_order_trac` (`id`,
																	   `order_id`,
																	   `track_status`,
																	   `assigned_user`)
																VALUES (NULL,
																		'".$order_id."', 
																		'".$track_status."', 
																		'".$assigned_to."');");
						
						}
					else{
						mysql_query("UPDATE  `wp_spec_order_trac` SET  
		`assigned_user` =  '".$assigned_to."' , `track_status` =  '".$track_status."' WHERE  `wp_spec_order_trac`.`order_id` ='".$order_id."'");
						
						}	
					//$this->send_mail_track($to=$assigned_to,$subject='pjct',$message='assigned',$headers,$attachments,$from=$assigned_by,$order_id,$mailtype='assign_project');
						
			}		
		}
		
		function update_status_time($order_id,$track_status){
		
		# SAVE STATUS CHANGE TIME
				$query_status = mysql_query('SELECT id FROM wp_spec_status_time WHERE order_id="'.$order_id.'" AND status ="'.$track_status.'"');
					if (mysql_num_rows($query_status) <= 0 ){
					$time = time();
					mysql_query("INSERT INTO `wp_spec_status_time` (`id`, `order_id`, `status`, `time`) VALUES (NULL, '".$order_id."', '".$track_status."', '".$time."');");
				}	
		}
		
		function get_status_time($order_id,$status){
			$query = mysql_query('SELECT time FROM wp_spec_status_time WHERE order_id='.$order_id.' AND status='.$status.'');
			$row   = mysql_fetch_array($query);
			return $row['time'];
		}
		function get_assigned_count($order_id){
			
		$query_trac = mysql_query('SELECT id FROM wp_spec_assigned_orders WHERE order_id="'.$order_id.'"');	
		return	mysql_num_rows($query_trac);
		
		}
		
		function saveMessage($assigned_by,$assign_to,$order_id,$message,$entered_time=0){
			if($entered_time == 0)
			$entered_time = time();
			if($message!='')
			mysql_query("INSERT INTO  `wp_spec_messages` (
									`id` ,
									`added_by` ,
									`send_to` ,
									`track_id` ,
									`message` ,
									`time`,
									`entered_time`
									
									)
									VALUES (
									NULL ,  '".$assigned_by."',  '".$assign_to."', '".$order_id."', '".$message."', 
									CURRENT_TIMESTAMP,'".$entered_time."'
									);");
			
			}
			
		function count_unread_message($order_id,$user_id){
			
			$query_cart = mysql_query('SELECT id FROM wp_spec_messages WHERE track_id="'.$order_id.'" AND send_to ="'.$user_id.'" AND read_status =0');
			return mysql_num_rows($query_cart);
			}	
			
		function count_purchase($user_id){
			
			$query_cart = mysql_query('SELECT id FROM wp_spec_checkout WHERE user_id="'.$user_id.'" AND payment_status=1');
			return mysql_num_rows($query_cart);
			}	
		function send_mail_track($to='',$subject='',$message='',$headers='',$attachments='',$from='',$order_id='',$mailtype=''){
			
			
			
			# GENERAL SETTINGS
				//$params = array( 'mail' => "yes");
				//$login_url   = add_query_arg( $params,site_url().'/login' );
			$website_url =  site_url();
			$login_link = '<a href="'.site_url().'/login/" ><b>Click Here</b></a>';
			//$admin_link = '<a href="'.site_url().'/wp-admin" ><b>Click Here</b></a>';
			$admin_link = '<a href="https://www.spectrumrealestatephotography.com/wp-admin/" ><b>Click Here</b></a>';
			
			
			$website_link = '<a href="'.$website_url.'" ><b>SpectrumRealEstatePhotography.com</b></a>';
		
			
			# TO USER DETAILS
			$user_info = get_userdata($to);
			$to_first_name		 = $user_info->first_name;
			$to_last_name		 = $user_info->last_name;
			$to_name 			 = $to_first_name.' '.$to_last_name;
			$to_email			 = $user_info->user_email;	
			$to_address 		 = $user_info->address;
			$to_city 			 = $user_info->city;
			$to_state			 = $user_info->state;
			$to_zip			 = $user_info->zip;
			$to = $to_email;
			
			# SEND FROM USER DETAILS
			if(isset($from)){
				$user_info = get_userdata($from);
				$from_first_name	 = $user_info->first_name;
				$from_last_name		 = $user_info->last_name;
				$from_name 			 = $from_first_name.' '.$from_last_name;
				$from_email			 = $user_info->user_email;	
				$from_address 		 = $user_info->address;
				$from_city 			 = $user_info->city;
				$from_state			 = $user_info->state;
				$from_zip			 = $user_info->zip;
				$from = $from_name.'<'.$from_email.'>';
				
			}
			else
				$from = 'SPECTRUM <office@SpectrumRealEstatePhotography.com>';
				
			# MAIL HEADERS
			//$headers[] = 'From : '.$from.'';
			//$headers[] = 'Bcc : sthomas@wbpro.com';
			
		
			# MAIL NOTIFICATION TO CUSTOMER
			if($mailtype=='order_confirmation'){
				# MAIL TO CUSTOMER
				$subject = 'Hello '.$to_name.', SPECTRUM  Purchase details: ORDER ID #'.$order_id;
				//<div align="left" class="show_print"><img src="'.get_template_directory_uri().'/images/logo.png" /></div>
				$mail_header   = '<div align="left" class="show_print"><img src="'.get_template_directory_uri().'/images/logo.png" width="200px" /></div>
								 <div style="font-family:"trebuchet ms",sans-serif;font-size:small;color:rgb(0,0,102)">
								  <h2 style="color:rgb(34,34,34);font-family:arial,clean,sans-serif;text-transform:uppercase;font-size:22px">ORDER CONFIRMATION</h2>
								  <div style="color:rgb(34,34,34);font-family:arial,clean,sans-serif;font-size:12px;margin-bottom:40px">
									<p>Dear '.$to_name.',</p>
									<p></p>
									<p>Thank you for shopping at '.$website_link.'. This is a confirmation that we have received your order placed with the details below.</p>
									<p>A representative will be contacting you within 24 hours to schedule the photo shoot.</p>
									<p></p>
									<p>You can view the status of your order and track the process from your account area, please '.$login_link.'.</p>
								  </div>';
				$message_body .= $mail_header.$message.'<br><br></div>';
				add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
				
				$this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
				//wp_mail( $to, $subject, $message_body, $headers);
				# MAIL TO ADMINISTRATOR
				
				
				$admin_name = get_option('blogname'); 
				$to			=  get_option('admin_email'); 
				# $admin_name =  'SPECTRUM'; 
				# $to			=  'office@spectrumrealestatephotography.com';
			
				$subject	= 'SPECTRUM New purchase notification';
				$message    = 'Hi Admin, <br> <br>New item has been purchased by a user, with ORDER ID#'.$order_id;
				$message   .= '<br><br>For more details '.$admin_link.'!';  				
				$this->SMTP_mailer($to,$admin_name,$subject, $message, $from_name,$from_email);
				//wp_mail( $to, $subject, $message, $headers);
			
			}
			
			else if($mailtype=='customer_status_update'){
				$subject = 'Hello '.$to_name.',SPECTRUM  Project status update ORDER ID #'.$order_id;
				$message_body   = '<div align="left" class="show_print"><img src="'.get_template_directory_uri().'/images/logo.png" width="200px" /></div>
							 <div style="font-family:"trebuchet ms",sans-serif;font-size:small;color:rgb(0,0,102)">
							  
							  <div style="color:rgb(34,34,34);font-family:arial,clean,sans-serif;font-size:12px;margin-bottom:40px">
								<p>Dear '.$to_name.',</p>
								<p></p>								
								';
				if(isset($message))
				$message_body .='<p>Your project has been updated with a new status:'.  $message.'<br><br></p>';
				$message_body .='<p>Please login to your account for more information, please '.$login_link.'.</p><br><br>
								Regards,<br><br>
								The Spectrum Team.
							    </div>';

				add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
		    	//wp_mail( $to, $subject, $message_body, $headers);
				$this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
				
				
				
			}
			else if($mailtype=='schedule_photoshoot'){
			
				$subject = 'Hello '.$to_name.',SPECTRUM  Photoshoot Scheduled ORDER ID #'.$order_id;
				$message_body   = '<div align="left" class="show_print"><img src="'.get_template_directory_uri().'/images/logo.png" width="200px" /></div>
							 <div style="font-family:"trebuchet ms",sans-serif;font-size:small;color:rgb(0,0,102)">
							  <h2 style="color:rgb(34,34,34);font-family:arial,clean,sans-serif;text-transform:uppercase;font-size:22px">STATUS UPDATE</h2>
							  <div style="color:rgb(34,34,34);font-family:arial,clean,sans-serif;font-size:12px;margin-bottom:40px">
								<p>Dear '.$to_name.',</p>
								<p></p>								
								<p>Your Spectrum project status has been updated.</p>';
				if(isset($message))
				$message_body .='<p>'.  $message.'<br><br></p>';
				$message_body .='<p>You can view the status of your order and track the process from your account area, please '.$login_link.'.</p><br><br>
								Regards,<br><br>
								The Spectrum Team.
							    </div>';

				add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
		    	//wp_mail( $to, $subject, $message_body, $headers);
				$this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
				
			
			}
			else if($mailtype=='tour_completed'){
			
				$subject = 'Hello '.$to_name.',SPECTRUM  Tour Completed ORDER ID #'.$order_id;
				
				/*$message_body   = '<div align="left" class="show_print"><img src="'.get_template_directory_uri().'/images/logo.png" width="200px" /></div>
							 <div style="font-family:"trebuchet ms",sans-serif;font-size:small;color:rgb(0,0,102)">
							  <h2 style="color:rgb(34,34,34);font-family:arial,clean,sans-serif;text-transform:uppercase;font-size:22px">STATUS UPDATE</h2>
							  <div style="color:rgb(34,34,34);font-family:arial,clean,sans-serif;font-size:12px;margin-bottom:40px">
								<p>Dear '.$to_name.',</p>
								<p></p>								
								<p>Your Spectrum project status has been updated. You can download the photos</p>';
				if(isset($message))
				$message_body .='<p>'.  $message.'<br><br></p>';
				$message_body .='<p>You can view the status of your order and track the process from your account area, please '.$login_link.'.</p><br><br>
								Regards,<br><br>
								The Spectrum Team.
							    </div>';*/
				$message_body = $message;
				add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
		    	//wp_mail( $to, $subject, $message_body, $headers);
				$this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
				
			
			}
			
			
			
			
			# NOTIFICATION TO PHOTOGRAPHER/DESIGNER
			else if($mailtype=='assign_project'){
				$subject = 'Hello '.$to_name.' SPECTRUM - New Project has been assigned to you: ORDER ID #'.$order_id;
				$message_body  = '<div align="left" class="show_print"><img src="'.get_template_directory_uri().'/images/logo.png" width="200px" /></div>';
				$message_body .= 'Hi,' .$to_name.'<br><br>';
				$message_body .= 'A new project has been assigned or the status on an existing project has been updated.<br><br>';
				if(isset($message))
					$message_body .=$message.'<br><br>';
				$message_body .= 'Please login to your account for additional information '.$login_link.'<br><br>' ;
				$message_body .= '<br>Thanks!' ;
				add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
		    	//wp_mail( $to, $subject, $message_body, $headers);
				$this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
			}
			else if($mailtype=='shoot_completed'){
				$subject = 'Hello '.$to_name.' SPECTRUM - New Project has been assigned to you: ORDER ID #'.$order_id;
				$message_body  = '<div align="left" class="show_print"><img src="'.get_template_directory_uri().'/images/logo.png" width="200px" /></div>';
				$message_body .= 'Hi,' .$to_name.'<br><br>';
				$message_body .= 'A new project has been assigned or the status on an existing project has been updated.<br><br>';
				if(isset($message))
					$message_body .=$message.'<br><br>';
				$message_body .= 'Please login to your account for additional information '.$login_link.'<br><br>' ;
				$message_body .= '<br>Thanks!' ;
				add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
		    	//wp_mail( $to, $subject, $message_body, $headers);
			    $this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
			}
			
			
			
			else if($mailtype=='payment_confirmation'){
				
				# MAIL TO CUSTOMER
				$subject = 'SPECTRUM - Payment details ORDER ID #'.$order_id;
				$message_body  = 'Hi,' .$to_name.'<br><br>';
				//$message_body .= 'You have successfully completed your order.<br><br>';
				if(isset($message))
					$message_body .='MESSAGE : '.  $message.'<br><br>';
					$message_body .= 'Please login to your Spectrum Account for more details!'.$login_link.'<br><br>' ;
					$message_body .= '<br>Thanks!<br>' ;
					add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
				//wp_mail( $to, $subject, $message_body, $headers);
				$this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
				
				}
			else if($mailtype=='login_informations')	{
					# MAIL TO CUSTOMER
				$subject = 'Hello '.$to_name.',SPECTRUM  Login Details';
				
				$message_body   = '<div align="left" class="show_print"><img src="'.get_template_directory_uri().'/images/logo.png" width="200px" /></div>
							 <div style="font-family:"trebuchet ms",sans-serif;font-size:small;color:rgb(0,0,102)">
							 <div style="color:rgb(34,34,34);font-family:arial,clean,sans-serif;font-size:12px;margin-bottom:40px">
								<p>Dear '.$to_name.',</p>
								<p></p>								
								<p>New Accound has been created</p>';
				
				$message_body .='<p>Login destails:<br><br>'.$message.'<br><br></p>';
				$message_body .='<p>Please login to your Spectrum Account for more details!  '.$login_link.'<br><br>
								Regards,<br><br>
								The Spectrum Team.
							    </div>';
				add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
				//wp_mail( $to, $subject, $message_body, $headers);	
				$this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
						
			}
			
		
	 }	
		//	$this->SMTP_mailer($to_email,$to_name,$subject, $message_body, $from_name,$from_email);
		function SMTP_mailer($to_email,$to_name,$subject,$message_body,$from_name,$from_email){
		
		$smtp==false;
			if($smtp==true){
			
			
			$mail = new PHPMailer;
			$mail->SMTPDebug = 3;                               // Enable verbose debug output
			$mail->isSMTP();                                      // Set mailer to use SMTP
			$mail->Host = '216.70.64.121';  // Specify main and backup SMTP servers
			$mail->SMTPAuth = true;                               // Enable SMTP authentication
			$mail->Username = 'office@spectrumrealestatephotography.com';                 // SMTP username
			$mail->Password = 'Chelsea1!';                           // SMTP password
			$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
			$mail->Port = 587;                                    // TCP port to connect to

			$mail->From = 'office@spectrumrealestatephotography.com';
			$mail->FromName = 'SPECTRUM';
			$mail->addAddress($to_email, $to_name);     // Add a recipient
			//$mail->addAddress('ellen@example.com');               // Name is optional
			$mail->addReplyTo($from_email, $from_name);
		//	$mail->addCC('cc@example.com');
			$mail->addBCC('sthomas@wbpro.com');

			//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
			//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
			$mail->isHTML(true);                                  // Set email format to HTML

			$mail->Subject = $subject;
			
			$mail->Body    = $message_body;
			$mail->AltBody = $message_body;
			//$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
			//$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

			$mail->send();
			if(!$mail->send()) {
				echo 'Message could not be sent.';
				echo 'Mailer Error: ' . $mail->ErrorInfo;
			} else {
				echo 'Message has been sent';
			}
			exit;
			}
		else {
			
			$headers[] = 'From : SPECTRUM <office@SpectrumRealEstatePhotography.com>';
			$headers[] = 'Bcc : sthomas@wbpro.com';
			$headers[] = 'Reply-to : '.$from_name.'<'.$from_email.'>';
			
			wp_mail( $to_email, $subject, $message_body, $headers);	
			
			/*// To send HTML mail, the Content-type header must be set
			$headers  = 'MIME-Version: 1.0' . "\r\n";
			$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

			// Additional headers
			$headers .= 'To: '.$to_name.' <'.$to_email.'>' . "\r\n";
			$headers .= 'From: SPECTRUM <office@SpectrumRealEstatePhotography.com>' . "\r\n";
			$headers .= 'Reply-to: '.$from_name.'  <'.$from_email.'>' . "\r\n";
			$headers .= 'Bcc: sthomas@wbpro.com' . "\r\n";
			
			// Mail it
			mail($to_email, $subject, $message_body, $headers);*/
		
		}	
		mysql_query("INSERT INTO  `wp_spec_mailtrac` (
					`id` ,
					`to_email` ,
					`to_name` ,
					`subject` ,
					`message_body` ,
					`from_name` ,
					`from_email`
					)
					VALUES (
					NULL ,  '$to_email',  '$to_name',  '$subject',  '$message_body',  '$from_name',  '$from_email'
					);
					");
		}
	}


?>